//
//  shopViewController.m
//  UI_HLJ_Frame
//
//  Created by dlios on 15-7-2.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "shopViewController.h"
#import "shopModel.h"
#import "shopCell.h"
#import "shopTableViewCell.h"
#import "shopUPCollectionViewCell.h"
#import "MerchantDetailsViewController.h"
#import "MBProgressHUD.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface shopViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UITableViewDelegate>
@property(nonatomic,retain)MBProgressHUD *hud;
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)UICollectionView *collectionViewUP;

@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)NSMutableArray *arr1;
@property(nonatomic, retain)NSMutableArray *arr2;
@property(nonatomic, retain)NSMutableArray *arr3;
@property(nonatomic, retain)NSMutableArray *arr4;
@property(nonatomic, retain)NSMutableArray *arr5;

@property(nonatomic, retain)NSMutableArray *shopUParr;
@property(nonatomic,assign)NSInteger flag;;

@end

@implementation shopViewController

#pragma mark 初始化 collectionViewUP
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        // 创建一个数组用来给label赋值
        self.shopUParr = [NSMutableArray arrayWithObjects:@"☞ 婚礼策划 ☜",@"☞ 婚纱摄影 ☜",@"☞ 摄影师 ☜",@"☞ 摄像师 ☜",@"☞ 化妆师 ☜",@"☞ 主持人 ☜", nil];
    }
    return self;
}


- (void)dealloc
{
    [_collectionView release];
    [_arr release];
    [_arr1 release];
    [_arr2 release];
    [_arr3 release];
    [_arr4 release];
    [_arr5 release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    
    // 建立UICollectionViewFlowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//    flowLayout.minimumInteritemSpacing=0;
    flowLayout.minimumLineSpacing = 0;
    
    // 建立UICollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT / 20, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:1.000 green:0.846 blue:0.848 alpha:1.000];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.tag = 1001;
    // 注册
    [self.collectionView registerClass:[shopCell class] forCellWithReuseIdentifier:@"shopView"];
    [self.view addSubview:self.collectionView];
    [self.collectionView release];
    self.arr = [NSMutableArray array];
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.contentOffset = CGPointMake(0, 0);
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.view addSubview:self.hud];
    self.hud.labelText = @"loading...";
    // 创建creat
    [self creat];
    

    // 建立collectionViewFlowLayout
    UICollectionViewFlowLayout *flowLayoutUP = [[UICollectionViewFlowLayout alloc] init];
    [flowLayoutUP setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    flowLayoutUP.itemSize = CGSizeMake(WIDTH , HEIGHT / 20);
    flowLayoutUP.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayoutUP.minimumLineSpacing = 0;
    
    // 建立上面的collectionView
    self.collectionViewUP = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH , HEIGHT / 20) collectionViewLayout:flowLayoutUP];
    self.collectionViewUP.backgroundColor = [UIColor whiteColor];
    self.collectionViewUP.delegate = self;
    self.collectionViewUP.dataSource = self;
    self.collectionViewUP.tag = 1000;
    [self.collectionViewUP registerClass:[shopUPCollectionViewCell class] forCellWithReuseIdentifier:@"shopViewUP"];
    [self.view addSubview:self.collectionViewUP];
    [self.collectionViewUP release];
    
    self.collectionViewUP.pagingEnabled = YES;
    self.collectionViewUP.contentOffset = CGPointMake(0, 0);
   
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (collectionView == self.collectionViewUP) {
        
        return self.shopUParr.count;
        
    } else {
        return 6;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collectionViewUP) {
        shopUPCollectionViewCell *cellUP = [collectionView dequeueReusableCellWithReuseIdentifier:@"shopViewUP" forIndexPath:indexPath];
        cellUP.backgroundColor = [UIColor colorWithRed:1.000 green:0.869 blue:0.885 alpha:0.650];
        
        
        cellUP.label.text = self.shopUParr[indexPath.row];
       
        return cellUP;
        
    } else {
        self.flag = indexPath.row;
    shopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"shopView" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor whiteColor];
        
    cell.tableView.delegate = self;
    
#pragma mark collectionView的数组值传到tableView的数组中
    if (indexPath.row == 0) {
        if (self.arr) {
            cell.arrShop = self.arr;
            cell.flag = indexPath.row;

        }
    } else if (indexPath.row == 1){
        if (self.arr1) {
            cell.arrShop1 = self.arr1;
            cell.flag = indexPath.row;
        }
    } else if (indexPath.row == 2){
        if (self.arr2) {
            cell.arrShop2 = self.arr2;
            cell.flag = indexPath.row;
        }
    } else if (indexPath.row == 3){
        if (self.arr3) {
            cell.arrShop3 = self.arr3;
            cell.flag = indexPath.row;
        }
    } else if (indexPath.row == 4){
        if (self.arr4) {
            cell.arrShop4 = self.arr4;
            cell.flag = indexPath.row;
        }
    } else {
        if (self.arr5) {
            cell.arrShop5 = self.arr5;
            cell.flag = indexPath.row;
        }
    }
   
    return cell;
}
}

#pragma mark 实现上下同时滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag == 1000) {
        
    } else {
        NSInteger index = scrollView.contentOffset.x / WIDTH;
        [self.collectionViewUP setContentOffset:CGPointMake(WIDTH  * index, 0) animated:YES];
    }
    if (scrollView.tag == 1001) {
        
    } else {
        NSInteger index = scrollView.contentOffset.x / WIDTH;
        [self.collectionView setContentOffset:CGPointMake(WIDTH * index, 0) animated:YES];
    }
}

- (void)creat{
    
#pragma mark 第一个解析
    NSURL *url = [NSURL URLWithString:self.shopStr];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSMutableDictionary *shopDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//        NSLog(@"%@", shopDic);
        NSMutableArray *shopArr = shopDic[@"merchants"];
        for (NSDictionary *tempDic in shopArr) {
            shopModel *shop = [[shopModel alloc] initWithDic:tempDic];
            [self.arr addObject:shop];
            [shop release];
            
        }
        [self.collectionView reloadData];
    }];
    
#pragma mark 第二个解析
    self.arr1 = [NSMutableArray array];
    
    NSURL *url1 = [NSURL URLWithString:self.shopStr1];
    NSMutableURLRequest * request1 = [NSMutableURLRequest requestWithURL:url1];
    [NSURLConnection sendAsynchronousRequest:request1 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSMutableDictionary *shopDic1 = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *shopArr1 = shopDic1[@"merchants"];
        for (NSDictionary *tempDic1 in shopArr1) {
            shopModel *shop1 = [[shopModel alloc] initWithDic:tempDic1];
            [self.arr1 addObject:shop1];
            [shop1 release];
        }
        [self.collectionView reloadData];
    }];
    
#pragma mark 第三个解析
    self.arr2 = [NSMutableArray array];
    
    NSURL *url2 = [NSURL URLWithString:self.shopStr2];
    NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:url2];
    [NSURLConnection sendAsynchronousRequest:request2 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        NSMutableDictionary *shopDic2 = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *shopArr2 = shopDic2[@"merchants"];
        for (NSDictionary *tempDic2 in shopArr2) {
            shopModel *shop2 = [[shopModel alloc] initWithDic:tempDic2];
            [self.arr2 addObject:shop2];
            [shop2 release];
        }
        [self.collectionView reloadData];
    }];
    
#pragma mark 第四个解析
    self.arr3 = [NSMutableArray array];
    
    NSURL *url3 = [NSURL URLWithString:self.shopStr3];
    NSMutableURLRequest *request3 = [NSMutableURLRequest requestWithURL:url3];
    [NSURLConnection sendAsynchronousRequest:request3 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSMutableDictionary *shopDic3 = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *shopArr3 = shopDic3[@"merchants"];
        for (NSDictionary *tempDic3 in shopArr3) {
            shopModel *shop3 = [[shopModel alloc] initWithDic:tempDic3];
            [self.arr3 addObject:shop3];
            [shop3 release];
        }
        [self.collectionView reloadData];
    }];
    
#pragma mark 第五个解析
    self.arr4 = [NSMutableArray array];
    
    NSURL *url4 = [NSURL URLWithString:self.shopStr4];
    NSMutableURLRequest *request4 = [NSMutableURLRequest requestWithURL:url4];
    [NSURLConnection sendAsynchronousRequest:request4 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSMutableDictionary *shopDic4 = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *shopArr4 = shopDic4[@"merchants"];
        for (NSDictionary *tempDic4 in shopArr4) {
            shopModel *shop4 = [[shopModel alloc] initWithDic:tempDic4];
            [self.arr4 addObject:shop4];
            [shop4 release];
        }
        [self.collectionView reloadData];
    }];
    
#pragma mark 第六个解析
    self.arr5 = [NSMutableArray array];
    
    NSURL *url5 = [NSURL URLWithString:self.shopStr5];
    NSMutableURLRequest *request5 = [NSMutableURLRequest requestWithURL:url5];
    [NSURLConnection sendAsynchronousRequest:request5 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSMutableDictionary *shopDic5 = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *shopArr5 = shopDic5[@"merchants"];
        for (NSDictionary *tempDic5 in shopArr5) {
            shopModel *shop5 = [[shopModel alloc] initWithDic:tempDic5];
            [self.arr5 addObject:shop5];
            [shop5 release];
        }
        [self.collectionView reloadData];
    }];

    
    [self.hud hide:YES afterDelay:1];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MerchantDetailsViewController *MDVC= [[MerchantDetailsViewController alloc]init];
    [self.navigationController pushViewController:MDVC animated:YES];
    if (self.flag==0) {
        shopModel *model=[[shopModel alloc]init];
        model= self.arr[indexPath.row];
        CGFloat a = [model.bid floatValue];
        MDVC.merchantIdStr= [NSNumber numberWithFloat:a];
        MDVC.coverStr = model.cover_path;
        MDVC.logoStr = model.logo_path;
        MDVC.nameStr = model.name;
        MDVC.addressStr = model.address;
        
    }else if(self.flag ==1)
    {      shopModel *model=[[shopModel alloc]init];
        model= self.arr1[indexPath.row];
        CGFloat a = [model.bid floatValue];
        MDVC.merchantIdStr= [NSNumber numberWithFloat:a];
        MDVC.coverStr = model.cover_path;
        MDVC.logoStr = model.logo_path;
        MDVC.nameStr = model.name;
        MDVC.addressStr = model.address;

        }
    else if(self.flag == 2)
    {      shopModel *model=[[shopModel alloc]init];
        model= self.arr2[indexPath.row];
        CGFloat a = [model.bid floatValue];
        MDVC.merchantIdStr= [NSNumber numberWithFloat:a];
        MDVC.coverStr = model.cover_path;
        MDVC.logoStr = model.logo_path;
        MDVC.nameStr = model.name;
        MDVC.addressStr = model.address;
 }
    else if(self.flag == 3)
    {
        shopModel *model=[[shopModel alloc]init];
        model= self.arr3[indexPath.row];
        CGFloat a = [model.bid floatValue];
        MDVC.merchantIdStr= [NSNumber numberWithFloat:a];
        MDVC.coverStr = model.cover_path;
        MDVC.logoStr = model.logo_path;
        MDVC.nameStr = model.name;
        MDVC.addressStr = model.address;
 }
    else if(self.flag == 4)
    {
        shopModel *model=[[shopModel alloc]init];
        model= self.arr4[indexPath.row];
        CGFloat a = [model.bid floatValue];
        MDVC.merchantIdStr= [NSNumber numberWithFloat:a];
        MDVC.coverStr = model.cover_path;
        MDVC.logoStr = model.logo_path;
        MDVC.nameStr = model.name;
        MDVC.addressStr = model.address;
   }
    else if(self.flag == 5)
    {
        shopModel *model=[[shopModel alloc]init];
        model= self.arr5[indexPath.row];
        CGFloat a = [model.bid floatValue];
        MDVC.merchantIdStr= [NSNumber numberWithFloat:a];
        MDVC.coverStr = model.cover_path;
        MDVC.logoStr = model.logo_path;
        MDVC.nameStr = model.name;
        MDVC.addressStr = model.address;
 }

   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
